home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / dev / lang / Python151_Src.lha / Python1.5_Source / Amiga / access.c < prev    next >
C/C++ Source or Header  |  1994-09-30  |  2KB  |  111 lines

  1. RCS_ID_C="$Id: access.c,v 4.2 1994/09/29 23:09:02 jraja Exp $"
  2. /*
  3.  *      access.c - check access to a file or a directory
  4.  *
  5.  *      Copyright © 1994 AmiTCP/IP Group, 
  6.  *                       Network Solutions Development Inc.
  7.  *                       All rights reserved.
  8.  */
  9.  
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <dos.h>
  13. #include <string.h>
  14. #include <errno.h>
  15. #include <dos/dos.h>
  16. #include <proto/dos.h>
  17. #include <proto/exec.h>
  18. #include <exec/memory.h>
  19.  
  20. #include <bsdsocket.h>
  21.  
  22. /*
  23.  * I know, the goto's are ugly, but they make the code smaller and help
  24.  * to prevent duplicating code.
  25.  */
  26. int
  27. access(const char *name, int mode)
  28. {
  29.   BPTR    lock, parentLock;
  30.   LONG    prot;
  31.   UBYTE   bytes[sizeof(struct FileInfoBlock) + sizeof(struct InfoData) + 3];
  32.   struct FileInfoBlock *fib;
  33.   struct InfoData      *info;
  34.  
  35.   /*
  36.    * align the data areas
  37.    */
  38.   fib = (struct FileInfoBlock *) (((ULONG) bytes+3) & (0xFFFFFFFF-3));
  39.   info = (struct InfoData *) (ULONG)(fib + 1);
  40.  
  41.   /*
  42.    * Lock the file (or directory)
  43.    */
  44.   if ((lock = Lock((STRPTR)name, SHARED_LOCK)) == NULL)
  45.     goto osfail;
  46.   
  47.   if (!Examine(lock, fib))
  48.     goto osfail;
  49.   
  50.   prot = fib->fib_Protection;
  51.  
  52.   /*
  53.    * Check each access mode
  54.    */
  55.   if (mode & R_OK && prot & FIBF_READ) {
  56.     errno = EACCES;
  57.     goto fail;
  58.   }
  59.   if (mode & W_OK) {
  60.     /*
  61.      * Check for write protected disks
  62.      */
  63.     if (!Info(lock, info))
  64.       goto osfail;
  65.  
  66.     if (info->id_DiskState == ID_WRITE_PROTECTED) {
  67.       errno = EROFS;
  68.       goto fail;
  69.     }
  70.  
  71.     /*
  72.      * not write protected: Check if the lock is to the root of the 
  73.      * disk, if it is, force writing to be allowed.
  74.      * Check if the lock is a directory before taking ParentDir()
  75.      */
  76.     if (fib->fib_DirEntryType >= 0) { /* lock is a directory */
  77.       parentLock = ParentDir(lock);
  78.       if (parentLock != NULL)
  79.     UnLock(parentLock); /* not the root, prot is valid */
  80.       else
  81.     prot &= ~FIBF_WRITE; /* the root, force writing to be allowed */
  82.     }
  83.     if (prot & FIBF_WRITE) {
  84.       errno = EACCES;
  85.       goto fail;
  86.     }
  87.   }
  88.   if (mode & X_OK && prot & FIBF_EXECUTE) {
  89.     errno = EACCES;
  90.     goto fail;
  91.   }
  92.   
  93.   /* F_OK */
  94.  
  95.   UnLock(lock);
  96.   return 0;
  97.   
  98.  osfail:
  99. #if __SASC
  100.   errno = __io2errno(_OSERR = IoErr());
  101. #else
  102.   _ug_set_errno(IoErr());
  103. #endif
  104.  
  105.  fail:
  106.   if (lock != NULL)
  107.     UnLock(lock);
  108.  
  109.   return -1;
  110. }
  111.